package com.cgbl.cg.view

import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.Switch
import android.widget.TextView
import androidx.appcompat.widget.AppCompatEditText
import androidx.appcompat.widget.AppCompatImageView
import com.cgbl.cg.R
import com.lxj.xpopup.core.BottomPopupView

class AddCollectPop(context: Context) : BottomPopupView(context) {
    private var confirmClickListener: (collectName: String, conllectInfo: String, status: String) -> Unit =
        { s: String, s1: String, s2: String ->

        }

    private var cancelClickListener: () -> Unit = {}

    private lateinit var tvConfirm: TextView
    private lateinit var ivCancel: AppCompatImageView
    private lateinit var tvCancel: TextView
    private lateinit var editContent: AppCompatEditText
    private lateinit var editTitle: AppCompatEditText
    private lateinit var tvErrorPrompt: TextView
    private lateinit var switchStatus: Switch

    override fun getImplLayoutId(): Int {
        return R.layout.app_pop_add_collect
    }

    override fun initPopupContent() {
        super.initPopupContent()
        tvCancel = findViewById(R.id.tv_cancel)
        tvConfirm = findViewById(R.id.tv_confirm)
        ivCancel = findViewById(R.id.iv_cancel)
        editTitle = findViewById(R.id.edit_title)
        editContent = findViewById(R.id.edit_content)
        tvErrorPrompt = findViewById(R.id.tv_error_prompt)
        switchStatus = findViewById(R.id.switch_status)
        editContent.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                tvErrorPrompt.visibility = if ((s?.length ?: 0) > 100) {
                    View.VISIBLE
                } else {
                    View.GONE
                }
            }

            override fun afterTextChanged(s: Editable?) {

            }

        })
        tvCancel.setOnClickListener {
            cancelClickListener.invoke()
        }

        ivCancel.setOnClickListener {
            cancelClickListener.invoke()
        }

        tvConfirm.setOnClickListener {
            confirmClickListener.invoke(
                editTitle.text.toString(),
                editContent.text.toString(),
                if (switchStatus.isChecked) {
                    "1"
                } else {
                    "0"
                }
            )
        }
    }

    fun setConfirmClickListener(click: (collectName: String, conllectInfo: String, status: String) -> Unit): AddCollectPop {
        confirmClickListener = click
        return this
    }

    fun setCancelClickListener(click: () -> Unit): AddCollectPop {
        cancelClickListener = click
        return this
    }
}